<?php

function application_management($form, &$form_state) {
  $header = array(
    'id' => array('data' => t('#'), 'field' => 'id', 'sort' => 'desc'),
    'title' => array('data' => t('Recruiment'), 'field' => 'title'),
    'mail' => array('data' => t('Email'), 'field' => 'mail'),
    'ip' => array('data' => t('IP'), 'field' => 'ip'),
    'applied' => array('data' => t('Applied'), 'field' => 'applied'),
    'operations' => array('data' => t('Operations'))
  );

  $form['recruitment'] = array(
    '#type' => 'select',
    '#title' => t('Recruitment'),
    '#options' => get_select_list_nids_by_type('recruitment'),
    '#default_value' => isset($_SESSION['application_search']['recruitment']) ? $_SESSION['application_search']['recruitment'] : ''
  );
  $form['email'] = array(
    '#type' => 'textfield',
    '#title' => t('Email'),
    '#autocomplete_path' => 'user-mail/autocomplete',
    '#default_value' => isset($_SESSION['application_search']['email']) ? $_SESSION['application_search']['email'] : ''
  );
  $form['search'] = array(
    '#type' => 'submit',
    '#value' => t('Search'),
  );
  $form['reset'] = array(
    '#type' => 'submit',
    '#value' => t('Reset'),
  );

  $results = get_all_aplications($header);
  $rows = array();
  if (count($results)) {
    $destination = drupal_get_destination();
    foreach ($results as $value) {
      $operations['view_cv'] = array(
        'title' => t('view cv'),
        'href' => 'admin/get-cv/' . $value['uid']
      );
      $operations['edit_profile'] = array(
        'title' => t('profile'),
        'href' => 'user/' . $value['uid'] . '/edit',
        'query' => $destination,
      );
      $rows[] = array(
        'id' => $value['id'],
        'title' => l($value['title'], 'node/' . $value['nid']),
        'mail' => $value['mail'],
        'ip' => $value['ip'],
        'applied' => $value['applied'],
        'operations' => array(
          'data' => array(
            '#theme' => 'links__node_operations',
            '#links' => $operations,
            '#attributes' => array('class' => array('links', 'inline')),
          ),
        )
      );
    }
  }
  $form['application'] = array(
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $rows,
    '#empty' => t('No content available.'),
  );
  $form['delete'] = array(
    '#type' => 'submit',
    '#value' => t('Delete'),
    '#submit' => array('delete_selected_application'),
  );
  $form['pager'] = array('#markup' => theme('pager'));
  return $form;
}

function application_management_submit($form, &$form_state) {
  if ($form_state['values']['op'] == t('Search')) {
    $_SESSION['application_search'] = array(
      'recruitment' => $form_state['values']['recruitment'],
      'email' => $form_state['values']['email'],
    );
  } else {
    unset($_SESSION['application_search']);
  }
}

function delete_selected_application($form, &$form_state) {
  $value = $form_state['values'];
  $application = isset($value['application']) ? $value['application'] : '';
  $application = array_filter($application);
  if (!empty($application) && count($application) > 0) {
    $result = db_delete('application')->condition('id', $application)->execute();
    if ($result) {
      drupal_set_message('Delete selected application successful', 'status');
    } else {
      drupal_set_message('Delete selected application fail', 'error');
    }
  }
}

/**
 * Implements of hook_form
 */
function get_list_cv_form($form, &$form_state) {
  $header = array(
    'id' => array('data' => t('#'), 'field' => 'u.uid', 'sort' => 'desc'),
    'mail' => array('data' => t('Email'), 'field' => 'mail'),
    'operations' => array('data' => t('Operations'))
  );
  $form['email'] = array(
    '#type' => 'textfield',
    '#title' => t('Email'),
    '#autocomplete_path' => 'user-mail/autocomplete',
    '#default_value' => isset($_SESSION['list_cv_search']['email']) ? $_SESSION['list_cv_search']['email'] : ''
  );
  $form['search'] = array(
    '#type' => 'submit',
    '#value' => t('Search'),
  );
  $form['reset'] = array(
    '#type' => 'submit',
    '#value' => t('Reset'),
  );
  $results = get_all_cv($header);
  $rows = array();
  if (count($results)) {
    $destination = drupal_get_destination();
    foreach ($results as $value) {
      $operations['view_cv'] = array(
        'title' => t('view cv'),
        'href' => 'admin/get-cv/' . $value->uid
      );
      $operations['edit_profile'] = array(
        'title' => t('profile'),
        'href' => 'user/' . $value->uid . '/edit',
        'query' => $destination,
      );
      $rows[] = array(
        'id' => $value->uid,
        'mail' => $value->mail,
        'operations' => array(
          'data' => array(
            '#theme' => 'links__node_operations',
            '#links' => $operations,
            '#attributes' => array('class' => array('links', 'inline')),
          ),
        )
      );
    }
  }
  $form['application'] = array(
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $rows,
    '#empty' => t('No content available.'),
  );
  $form['delete'] = array(
    '#type' => 'submit',
    '#value' => t('Delete'),
    '#submit' => array('delete_selected_application'),
  );
  $form['pager'] = array('#markup' => theme('pager'));
  return $form;
}

/**
 * Implements of hook_form_submit
 */
function get_list_cv_form_submit($form, &$form_state) {
  if ($form_state['values']['op'] == t('Search')) {
    $_SESSION['list_cv_search'] = array(
      'email' => $form_state['values']['email']
    );
  } else {
    unset($_SESSION['list_cv_search']);
  }
}
